package o;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import okhttp3.Route;

public final class fvm {
    private final List<Route> ʻ = new ArrayList();
    private final List<InetAddress> ʼ;
    private boolean ʽ;
    private final ftt ˊ;
    private final fvn ˋ;
    private List<Proxy> ˎ = Collections.emptyList();
    private final ftz ˏ;
    private final fum ॱ;
    private List<InetSocketAddress> ॱॱ = Collections.emptyList();
    private int ᐝ;

    public static final class b {
        private boolean ˊ;
        private InetSocketAddress ˋ = null;
        private int ˎ = 0;
        private final fvn ˏ;
        private final List<Route> ॱ;

        b(List<Route> list, boolean z, fvn o_fvn) {
            this.ॱ = list;
            this.ˊ = z;
            this.ˏ = o_fvn;
        }

        public boolean ˋ() {
            if (this.ˊ) {
                if (this.ॱ.size() > 0) {
                    return true;
                }
                return false;
            } else if (this.ˎ >= this.ॱ.size()) {
                return false;
            } else {
                return true;
            }
        }

        public Route ॱ() {
            if (!ˋ()) {
                throw new NoSuchElementException();
            } else if (this.ˊ) {
                return (Route) this.ॱ.get(0);
            } else {
                List list = this.ॱ;
                int i = this.ˎ;
                this.ˎ = i + 1;
                return (Route) list.get(i);
            }
        }

        public List<Route> ˊ() {
            return new ArrayList(this.ॱ);
        }

        public boolean ˎ() {
            return this.ˊ;
        }

        public ArrayList<InetSocketAddress> ˏ() {
            ArrayList<InetSocketAddress> arrayList = new ArrayList();
            for (Route socketAddress : this.ॱ) {
                arrayList.add(socketAddress.socketAddress());
            }
            return arrayList;
        }

        private Route ˊ(InetSocketAddress inetSocketAddress) {
            Iterator it = this.ॱ.iterator();
            while (it.hasNext()) {
                Route route = (Route) it.next();
                if (route.socketAddress().equals(inetSocketAddress)) {
                    it.remove();
                    return route;
                }
            }
            return null;
        }

        public void ʼ() {
            if (this.ˊ && this.ˋ != null) {
                Route ˊ = ˊ(this.ˋ);
                if (ˊ != null) {
                    this.ˏ.ˊ(ˊ);
                }
                this.ˋ = null;
            }
        }

        public void ॱ(Route route) {
            if (!this.ˊ) {
                this.ˏ.ॱ(route);
            } else if (this.ˋ != null) {
                Route ˊ = ˊ(this.ˋ);
                if (ˊ != null) {
                    this.ˏ.ॱ(ˊ);
                }
                this.ˋ = null;
            }
        }

        public void ॱ(InetSocketAddress inetSocketAddress) {
            if (this.ˊ) {
                this.ˋ = inetSocketAddress;
            }
        }

        public void ॱ(ArrayList<InetSocketAddress> arrayList) {
            if (this.ˊ) {
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    Route ˊ = ˊ((InetSocketAddress) it.next());
                    if (ˊ != null) {
                        this.ˏ.ˊ(ˊ);
                    }
                }
            }
        }
    }

    public fvm(ftt o_ftt, fvn o_fvn, ftz o_ftz, fum o_fum) {
        this.ˊ = o_ftt;
        this.ˋ = o_fvn;
        this.ˏ = o_ftz;
        this.ॱ = o_fum;
        this.ʽ = o_ftz.request().ˊ();
        this.ʼ = o_ftz.request().ʻ();
        ॱ(o_ftt.ˊ(), o_ftt.ʽ());
        if (!this.ʽ) {
            return;
        }
        if (this.ˎ.size() > 1 || (this.ˎ.size() == 1 && ((Proxy) this.ˎ.get(0)).type() != Type.DIRECT)) {
            this.ʽ = false;
        }
    }

    public boolean ˋ() {
        return ˊ() || !this.ʻ.isEmpty();
    }

    public b ॱ() throws IOException {
        if (ˋ()) {
            List arrayList = new ArrayList();
            while (ˊ()) {
                Proxy ˎ = ˎ();
                int size = this.ॱॱ.size();
                for (int i = 0; i < size; i++) {
                    Route route = new Route(this.ˊ, ˎ, (InetSocketAddress) this.ॱॱ.get(i));
                    if (this.ˋ.ˎ(route)) {
                        this.ʻ.add(route);
                    } else {
                        arrayList.add(route);
                    }
                }
                if (!arrayList.isEmpty()) {
                    break;
                }
            }
            if (this.ʽ || arrayList.isEmpty()) {
                arrayList.addAll(this.ʻ);
                this.ʻ.clear();
            }
            return new b(arrayList, this.ʽ, this.ˋ);
        }
        throw new NoSuchElementException();
    }

    public void ॱ(Route route, IOException iOException) {
        if (!(route.proxy().type() == Type.DIRECT || this.ˊ.ᐝ() == null)) {
            this.ˊ.ᐝ().connectFailed(this.ˊ.ˊ().ˏ(), route.proxy().address(), iOException);
        }
        if (!this.ʽ) {
            this.ˋ.ˊ(route);
        }
    }

    private void ॱ(fur o_fur, Proxy proxy) {
        if (proxy != null) {
            this.ˎ = Collections.singletonList(proxy);
        } else {
            List select = this.ˊ.ᐝ().select(o_fur.ˏ());
            if (select == null || select.isEmpty()) {
                select = fvg.ˋ(Proxy.NO_PROXY);
            } else {
                select = fvg.ˏ(select);
            }
            this.ˎ = select;
        }
        this.ᐝ = 0;
    }

    private boolean ˊ() {
        return this.ᐝ < this.ˎ.size();
    }

    private Proxy ˎ() throws IOException {
        if (ˊ()) {
            List list = this.ˎ;
            int i = this.ᐝ;
            this.ᐝ = i + 1;
            Proxy proxy = (Proxy) list.get(i);
            ˊ(proxy);
            return proxy;
        }
        throw new SocketException("No route to " + this.ˊ.ˊ().ʻ() + "; exhausted proxy configurations: " + this.ˎ);
    }

    private void ˊ(Proxy proxy) throws IOException {
        int ॱॱ;
        String str;
        this.ॱॱ = new ArrayList();
        String ʻ;
        if (proxy.type() == Type.DIRECT || proxy.type() == Type.SOCKS) {
            ʻ = this.ˊ.ˊ().ʻ();
            ॱॱ = this.ˊ.ˊ().ॱॱ();
            str = ʻ;
        } else {
            SocketAddress address = proxy.address();
            if (address instanceof InetSocketAddress) {
                InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
                ʻ = ˏ(inetSocketAddress);
                ॱॱ = inetSocketAddress.getPort();
                str = ʻ;
            } else {
                throw new IllegalArgumentException("Proxy.address() is not an InetSocketAddress: " + address.getClass());
            }
        }
        if (ॱॱ < 1 || ॱॱ > 65535) {
            throw new SocketException("No route to " + str + ":" + ॱॱ + "; port is out of range");
        }
        if (proxy.type() == Type.SOCKS) {
            this.ॱॱ.add(InetSocketAddress.createUnresolved(str, ॱॱ));
        } else if (!this.ʽ || this.ʼ.isEmpty()) {
            this.ॱ.dnsStart(this.ˏ, str);
            List lookup = this.ˊ.ˎ().lookup(str);
            if (lookup.isEmpty()) {
                throw new UnknownHostException(this.ˊ.ˎ() + " returned no addresses for " + str);
            }
            this.ॱ.dnsEnd(this.ˏ, str, lookup);
            int size = lookup.size();
            for (int i = 0; i < size; i++) {
                this.ॱॱ.add(new InetSocketAddress((InetAddress) lookup.get(i), ॱॱ));
            }
        }
        ˋ(ॱॱ);
    }

    private void ˋ(int i) {
        if (this.ʽ) {
            if (this.ʼ != null) {
                for (int size = this.ʼ.size() - 1; size >= 0; size--) {
                    InetSocketAddress inetSocketAddress = new InetSocketAddress((InetAddress) this.ʼ.get(size), i);
                    if (this.ॱॱ.contains(inetSocketAddress)) {
                        this.ॱॱ.remove(inetSocketAddress);
                    }
                    this.ॱॱ.add(0, inetSocketAddress);
                }
            }
            if (this.ॱॱ.size() == 1) {
                this.ʽ = false;
            }
        }
    }

    static String ˏ(InetSocketAddress inetSocketAddress) {
        InetAddress address = inetSocketAddress.getAddress();
        if (address == null) {
            return inetSocketAddress.getHostName();
        }
        return address.getHostAddress();
    }
}
